package com.kordar.admin.modular.sys.entity;

import com.baomidou.mybatisplus.annotation.*;
import com.kordar.admin.kernel.annotation.Unique;
import com.kordar.admin.modular.sys.enums.AdminStatus;
import com.kordar.admin.modular.sys.enums.AdminType;
import com.kordar.admin.modular.sys.formatter.AdminEnumFormatter;
import com.kordar.admin.modular.sys.service.AdminService;
import com.kordar.datalist.DataColumn;
import com.kordar.datalist.ExcelDataColumn;
import com.kordar.datalist.Label;
import com.kordar.datalist.formatter.CreateTimeFormatter;
import com.kordar.datalist.formatter.IntegerFormatter;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.RandomUtils;
import org.apache.shiro.crypto.hash.Md5Hash;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import java.util.Date;

@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName("sys_admin")
public class AdminEntity {

    @DataColumn
    @TableId(type = IdType.ASSIGN_ID)
    private Long id = 0L;

    @Label("手机号码")
    @ExcelDataColumn
    @DataColumn
    @NotNull
    @NotEmpty(message = "手机号不能为空")
    @Pattern(regexp = "^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(17[013678])|(18[0,5-9]))\\d{8}$", message = "请输入正确的手机号")
    @Unique(field = "phone", message = "手机号已存在", classz = AdminService.class)
    private String phone;

    private String avatar;

    @Label("用户名")
    @ExcelDataColumn
    @DataColumn
    @NotEmpty(message = "用户名不能为空")
    @Pattern(regexp = "[0-9a-zA-Z_]{6,24}", message = "用户名只能包含数字、字母和_‘下划线’组合的6-24位字符")
    @Unique(field = "username", message = "账号已存在", classz = AdminService.class)
    private String username;

    private String password;

    @Label("状态")
    @ExcelDataColumn(formatter = AdminEnumFormatter.class, params = "status")
    @DataColumn(formatter = IntegerFormatter.class)
    private Integer status = AdminStatus.STATUS_ACTIVE.getCode();

    @Label("类型")
    @ExcelDataColumn(formatter = AdminEnumFormatter.class, params = "type")
    @DataColumn(formatter = IntegerFormatter.class)
    private Integer type = AdminType.TYPE_NORMAL.getCode();

    private String salt;

    @Version
    private Integer version;

    @Label("创建时间")
    @ExcelDataColumn(formatter = CreateTimeFormatter.class)
    @DataColumn(formatter = CreateTimeFormatter.class)
    @TableField(fill = FieldFill.INSERT)
    private Date createTime;

    @Label("更新时间")
    @ExcelDataColumn(formatter = CreateTimeFormatter.class)
    @TableField(fill = FieldFill.INSERT_UPDATE)
    private Date updateTime;

    public void generatePassword(String password) {
        int i = RandomUtils.nextInt(100000, 999999);
        setSalt(Integer.toString(i));
        Md5Hash md5 = new Md5Hash(password, this.getSalt());
        setPassword(md5.toString());
    }

    public void generateAvatar() {
        int number = RandomUtils.nextInt(1, 13);
        this.setAvatar("/avatars/" + String.format("%02d", number) + ".png");
    }
}
